//index.js
const app = getApp()

Page({
  data: {

    user:null,

    // 刷新加载
    loadingMoreHidden:true,
    page:1,
    limit:10,
    totalPages:1,
    hideHeader: true,
    hideBottom: true,
    merchantList:[],
    locationInfo:{},
    name:'',
  },


  onLoad: function () {
    
  },
  onShow: function (e) {
    let _this = this;
    _this.setData({
      user: app.getUser(),
    });
    // 是否从缓存取位置对象
    let locationInfo = wx.getStorageSync('location');
    if(locationInfo.street){
      locationInfo.address = locationInfo.street;
    };
    this.setData({
      locationInfo:locationInfo
    });
    this.getMerchantList();
    if (_this.data.user.id == null) return;
  },
  //加载
  onReachBottom: function() {
    let that = this;
    that.setData({
      page: that.data.page + 1,
    });
    if(that.data.page < that.data.totalPages || that.data.page == that.data.totalPages){
      this.getMerchantList();
      that.setData({
        hideBottom: false
      });
    }
    setTimeout(function () {
      that.setData({
        hideBottom: true
      })
    }, 1000);
  },
  //刷新
  onPullDownRefresh: function() {
    let that = this;
    that.setData({
      page: 1
    });
    that.setData({
      hideHeader: false,
      loadingMoreHidden: true,
    })
    setTimeout(function () {
      var date = new Date();
      that.setData({
        page: 1,
        hideHeader: true
      })
      that.getMerchantList()
    }, 1000);
    wx.stopPullDownRefresh()
  },
  //获取商户列表
  getMerchantList:function(){
    let that = this;
    let user = wx.getStorageSync('userInfo') ? JSON.parse(decodeURI(wx.getStorageSync('userInfo'))) : app.globalData.userInfo;
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/collect/list',
      data: {
        size:that.data.limit,
        page:that.data.page,
        type:'centre',
        mid:user.id
      },
    }).then(res =>{
      var tempArray = that.data.merchantList;
      var totalpage = totalpage = Math.ceil(parseInt(res.data.data.total) / that.data.limit);
      for (let key in res.data.data.rows) {
        if(res.data.data.rows[key].juli > 1000){
          res.data.data.rows[key].juli = (res.data.data.rows[key].juli / 1000).toFixed(1) + 'km'
        }else{
          res.data.data.rows[key].juli = res.data.data.rows[key].juli + 'm'
        };
      };
      that.setData({
        "merchantList":res.data.data.rows,
        'totalPages':totalpage,
      });
      let merchantList = [];
      if (that.data.page == 1) {
        that.setData({
          'merchantList': res.data.data.rows,
        })
      } else {
        tempArray = tempArray.concat(res.data.data.rows);
        that.setData({
          loadingMoreHidden: true,
          merchantList: tempArray,
        });
      };
      tempArray = [];
      if(res.data.data.rows.length != that.data.limit){
        that.setData({
          loadingMoreHidden: false,
        });
      }
    })
  },  
  onSearch:function(e){
    console.log(e);
    let that = this;
    that.setData({
      name:e.detail,
      page:1,
    });
    that.getMerchantList();
  },
  // jumpDetail: function (event) {
  //   let centreid = event.currentTarget.dataset['centreid'];
  //   wx.navigateTo({
  //     url: '/pages/store/store?centreid='+centreid,
  //   })
  // },
})
